package org.ulht.lei.redes;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class EchoClient2 {	
	public static void main(String args[]) throws Exception {
		if (args.length !=1){
			System.err.println ("usage: java EchoClient2 <host>");
			System.exit(1);
		}				
		String host = args[0];
		int port = 6500;
		String cmd, line;
		Socket socket = new Socket(host,port);
		BufferedReader input = new BufferedReader(
				new InputStreamReader(socket.getInputStream()));
		PrintStream output = new PrintStream(socket.getOutputStream(),true);
		while( true ) {//inicio do ciclo 			
			Scanner scan = new Scanner (System.in); 
			System.out.print(host+":"+port+"#>"); //linha de comandos 			
			cmd = scan.nextLine();// ler comando a enviar	
			output.println(cmd); //envia comando ao servidor  
			if ( cmd.equalsIgnoreCase("quit")){
				System.out.println("a sair..");
				break;//sair do ciclo.
			} 			
			while (!(line = input.readLine()).equals("***CLOSE***")) {//ciclo de input							
			System.out.println (line);//imprime resposta			
			}					
		}//fim ciclo
		input.close();//termina input
		output.close();//termina output
		socket.close();// termina socket	
	}
}